package service

import (
	"github.com/gin-gonic/gin"
	"talentsystem/model"
	"github.com/astaxie/beego/orm"
)

func PerfectEnrol (c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	err,result:=IsHaveEnrolTool(param.Id)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新重试",
		})
		return
	}
	var msg model.Enrol
	if result>0{
		o:=orm.NewOrm()
		err=o.QueryTable("enrol").Filter("account_id",param.Id).One(&msg)
		if err!=nil {
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新重试",
			})
			return
		}
	}
	c.JSON(200,gin.H{
		"status":200,
		"data":msg,
	})
	return
}
func UpdateErnrol(c*gin.Context){
	var param model.InsertEnrol
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	err,result:=IsHaveEnrolTool(param.AccountId)
	if result>0{
	o:=orm.NewOrm()
	err=o.Begin()
	if err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新重试",
		})
		return
	}
	num,err:=o.QueryTable("enrol").Filter("account_id",param.AccountId).Update(orm.Params{
		"Name": param.Name,
		"Sex": param.Sex,
		"Phone": param.Phone,
		"Age": param.Age,
	})
	if err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新重试",
		})
		return
	}
	if num>1{
		o.Rollback()
		c.JSON(403,gin.H{
			"status":403,
			"msg":"信息更新失败",
		})
		return
	}
	o.Commit()
}else {
		err,_:=InsertEnrol(param)
		if err!=nil {
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新重试",
			})
			return
		}
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
	})
	return
}
func EnrolLog(c*gin.Context){
	var param model.EnrolLogTemp
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	num,err:=orm.NewOrm().QueryTable("enrol_log").Filter("account_id",param.Accountid).Filter("position_id",param.Positionid).Filter("date",param.Date).Count()
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新重试",
		})
	}
	if num ==0{
		 enrollog:=new(model.EnrolLog)
		 enrollog.AccountId=param.Accountid
		 enrollog.PositionId=param.Positionid
		 enrollog.Fphone=param.Fphone
		 enrollog.Date=param.Date
		 o:=orm.NewOrm()
		 err:=o.Begin()
		 if err!=nil {
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新重试",
			})
			return
		}
		_,err=o.Insert(enrollog)
		if err!=nil {
			o.Rollback()
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新重试",
			})
		}
		o.Commit()
	}else if num>0{
		c.JSON(403, gin.H{
			"status": 403,
			"msg":    "你今天已经报名该职位了",
		})
		return
	}else{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新重试",
		})
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":"报名成功",
	})
}
func GetEnrolLog(c*gin.Context){
	var param model.GetAccountApplyJob
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	offset:=(param.Page-1)*param.Pagesize
	var result []model.GetEnrolLog
	_,err:=orm.NewOrm().Raw("SELECT enrol_log.id,enrol_log.account_id,enrol_log.position_id,enrol_log.fphone,enrol_log.`status`,enrol_log.date,enrol_log.remark,enrol.name,enrol.phone,enrol.sex,enrol.age,position.company,position.position FROM position,enrol,enrol_log WHERE enrol_log.account_id=enrol.account_id AND enrol_log.position_id=position.id ORDER BY enrol_log.id DESC LIMIT ? OFFSET ?",param.Pagesize,offset).QueryRows(&result)
	if err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	num,err:=orm.NewOrm().QueryTable("enrol_log").Count()
	if err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":result,
		"total":num,
	})
	return
}
func UpdateEnrolLogStatus (c*gin.Context){
	var param model.TypeUpdateResumeLog
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	err,state:=UpdateEnrolLogTools(param.Status,param.Id,param.Remark)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":state,
	})
}
func UpdateEnrolLogTools(state int64,id int64,remark string)(err error,status int64){
	o:=orm.NewOrm()
	_,err=o.QueryTable("enrol_log").Filter("id",id).Update(orm.Params{
		"Status":state,
		"Remark":remark,
	})
	if err!= nil{
		return err,0
	}
	return nil,1
}
func IsHaveEnrolTool(id int64)(err error,result int64){
	o:=orm.NewOrm()
	result,err=o.QueryTable("enrol").Filter("account_id",id).Count()
	return err,result
}
func UpdateTool(msg model.Enrol,id int64)(err error,result int64){
	o:=orm.NewOrm()
	result,err=o.QueryTable("enrol").Filter("account_id",id).Update(orm.Params{
		"Name": msg.Name,
		"Sex": msg.Sex,
		"Phone": msg.Phone,
		"Age": msg.Age,
	})
	return err,result
}
func InsertEnrol (msg model.InsertEnrol)(err error,state int64){
	enrol:=new(model.Enrol)
	enrol.Name=msg.Name
	enrol.Sex=msg.Sex
	enrol.Phone=msg.Phone
	enrol.Age=msg.Age
	enrol.AccountId=msg.AccountId
	o:=orm.NewOrm()
	err=o.Begin()
	if err!=nil {
		return err,0
	}
	_,err=o.Insert(enrol)
	if err!=nil {
		o.Rollback()
		return err,0
	}
	o.Commit()
	return nil,1
}
